//
//  YWKRegisterViewController.m
//  YWKUser
//
//  Created by renyi on 4/7/16.
//  Copyright © 2016 iCareTech. All rights reserved.
//

#import "YWKRegisterViewController.h"
#import "YWKCustomTextField.h"
#import "YWKDrawLineView.h"
#import "YWKLable.h"
#import "YWKHelper.h"
#import "FiexibleTool.h"
#import "UIColor+ICT.h"
#import <Masonry.h>
#import "YWKConfiguration.h"
#import "NSString+YWKChange.h"
#import "YWKDrawLineView.h"
#import "UIView+YWKFrameExtension.h"
#import "YWKLoginAndRegistViewModel.h"
#import "YWKLoginAndRegistModel.h"
#import "YWKLoginHander.h"
#import "YWKUser.h"
#import "MBProgressHUD+YWK.h"
#import <RealReachability.h>
#import "DDTTYLogger.h"
#import "DDASLLogger.h"

#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_ERROR;
#endif

#define labWidth       85
#define agreeServiceTermsText   @"点击注册，即代表你同意 云卫康用户协议"
#define YWKServiceTerms         @"云卫康用户协议"

#define kPhoneNumberTag              200
#define zCodeCountTime               60
#define kPlaceHolderColor [UIColor colorWithHex:0xDCDEE0]
//适配ios9尺寸，不能在viewdidload、loadview里面使用
#define IOS9VIEWFRAMEORIGIN ([[[UIDevice currentDevice] systemVersion] compare:@"9.0" options:NSNumericSearch] != NSOrderedAscending ? 64 : 0)
#define kIsChinese  [[NSString getPreferredLanguage] hasPrefix:@"zh-"]


@interface YWKRegisterViewController ()<
UITextFieldDelegate,
UIScrollViewDelegate,
UITextViewDelegate,
YWKLoginAndRegistModelDelegate,
YWKLoginHander
>

{
    NSInteger _count;
    float     _pwdTFWidth;        //输入密码的文本框长度
    float     _identifyTFWidth;   //输入验证码的文本框长度
    NSTimer   *_timer;
    BOOL      _isAccoutChecking;
    NSString  *_userN;           //账号名称
}

typedef NS_ENUM(NSInteger, YWKUserRoleType)
{
    YWKPatient = 0,      // 患者
    YWKDoctor,      // 医生
    YWKAssistant,      // 医助
    YWKSales,     // 业务员
    YWKX,     // X
    YWKSpo2doctor,     // 血氧检测医生
};

@property (nonatomic, strong) UIImageView *yWKLogoView;                 //C＋图
@property (nonatomic, strong) UIScrollView *rigisterScrollView;
@property (nonatomic, strong) UIView * scrollContentView;
//@property (nonatomic, strong) ICTOFClient *rigisterUser;
@property (nonatomic, strong) YWKCustomTextField *phoneNumberTextField; //手机号
@property (nonatomic, strong) YWKCustomTextField *identifyingCodeTF;    //验证码
@property (nonatomic, strong) YWKCustomTextField *pwdTextField;         //密码(设置密码)
@property (nonatomic, strong) YWKCustomTextField *ensurePwdTextField;   //确认密码

//
@property (nonatomic, strong) YWKDrawLineView * phoneSpareLine;
@property (nonatomic, strong) YWKDrawLineView * identySpareLine;
@property (nonatomic, strong) YWKDrawLineView * pwdSpareLine;
@property (nonatomic, strong) YWKDrawLineView * ensurePwdSpareLine;

@property (nonatomic, strong) UILabel * phoneNumberLable;
@property (nonatomic, strong) UILabel * identifyingCodeLable;
@property (nonatomic, strong) UILabel * pwdLable;
@property (nonatomic, strong) UILabel * ensurePwdLabel;
@property (nonatomic, strong) UITextView * userAgreementTV;
@property (nonatomic, strong) UIButton * identifyingBtn;                 //获取验证码
@property (nonatomic, strong) UIButton * rigterBtn;                      //确定or注册
@property (nonatomic, strong) UILabel * countDownTimer;
@property (nonatomic, assign) CGSize kbSize;
@property (nonatomic, strong)UIView * serviceTerms;
@property (nonatomic, strong)NSString * phoneNumber;

@property (nonatomic, strong)YWKLoginAndRegistViewModel * viewModel;
@property (nonatomic, strong)YWKLoginAndRegistModel * dataModel;
@property (nonatomic, strong)YWKLoginHander * loginHander;
@property (nonatomic, strong)MBProgressHUD *HUD;
@end

@implementation YWKRegisterViewController

#pragma mark - life cycle

- (id)init
{
    self = [super init];
    if (self) {
        //        _isAccoutChecking = NO;
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    
    if (self.pwdType == YWKFromLoginRegistVC)
    {
        self.title = @"注册";
    }
    else
    {
        if (self.pwdType == YWKFromLoginFindBackVC)
        {
            self.title = @"找回密码";
        }
        else
        {
            self.title = @"修改密码";
        }
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setEdgesForExtendedLayout:UIRectEdgeNone];
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:@{
                                                                      NSFontAttributeName:[UIFont systemFontOfSize:[FiexibleTool floatWithOriginalFloat:16.f
                                                                                                                                       isFlexibleHeight:YES]],
                                        NSForegroundColorAttributeName:[UIColor blackColor]
                                                                     }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.view addSubview:self.rigisterScrollView];
    [self.rigisterScrollView addSubview:self.scrollContentView];
    [self addConstraint];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
}


#pragma mark - YWKLoginAndRegistModelDelegate  网络响应回调

/**
 *  手机号或者账号是否存在
 *
 */
-(void)checkUserNameHasexisted:(NSDictionary *)response{
    
    NSInteger result = [response[@"result"] integerValue];
    NSString *sendString;
    /**
     *  注册时验证码传的是手机号
     */
    if (self.pwdType == YWKFromLoginRegistVC)
    {
        if (result == 20)
        {
            sendString = NSLocalizedString(@"该帐号已存在", nil);
            self.identifyingBtn.userInteractionEnabled = YES;
        }
        else if (result == 0)
        {
            sendString = NSLocalizedString(@"手机验证码已发送请查看手机短信", nil);
            [self countDown];
            [self.dataModel requestVerificationCodeAPIWithTelephone:self.phoneNumber];
        }
    }
    
    if (self.pwdType == YWKFromLoginFindBackVC || self.pwdType == YWKFromSetVC)
    {
        if (result == 20) {
            
            sendString = NSLocalizedString(@"手机验证码已发送请查看手机短信", nil);
            [self countDown];
            [self.dataModel requestVerificationCodeAPIWithName:self.phoneNumber];
        }
        else if (result == 0)
        {
            sendString = NSLocalizedString(@"帐号或手机号没注册！", nil);
            self.identifyingBtn.userInteractionEnabled = YES;
        }
    }
    [self showAlertWithString:sendString];
}


-(void)registResult:(NSDictionary *)response{
    
    NSInteger result = [response[@"result"] integerValue];
    if (result == 0) {
       
        _userN = response[@"username"];
        self.HUD.labelText = @"注册成功";
        [self configNewUser:self.phoneNumber password:self.pwdTextField.text phoneNumber:self.phoneNumber];
    }
    else if(result == 21)
    {
        [self.HUD removeFromSuperview];
        [self showAlertWithString:@"无效验证码"];
    }
}

/**
 *  返回验证码正确
 *
 */
-(void)getVerificationCode:(NSString *)code{
    
    self.identifyingCodeTF.text = code;
    [self.HUD removeFromSuperview];
}

/**
 *  返回验证码失败
 *
 *  @param response
 */
-(void)getVerificationCodeFailed{
    
    [MBProgressHUD hubWithTitle:NSLocalizedString(@"获取验证码失败", nil) second:3 toView:self.view];
}

-(void)modifyPasswordResult:(NSDictionary *)response{
    
    NSInteger result = [response[@"result"] integerValue];
    if (result == 0) {
        
        //修改后就要用保存密码
        self.loginHander = [YWKLoginHander loginHandler];
        YWKUser * currentUser = self.loginHander.account;
        currentUser.password = self.pwdTextField.text;
        [self.loginHander.account savePassword];
        
        self.HUD.mode = MBProgressHUDModeCustomView;
        UIImage *image = [UIImage imageNamed:@"checkout_white"];
        UIImageView * imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
        imageview.image = image;
        self.HUD.customView = imageview;
        self.HUD.labelFont = [UIFont fontWithName:kTextFontName
                                             size:[FiexibleTool floatWithOriginalFloat:14.f
                                                                      isFlexibleHeight:YES]];
        if (self.pwdType == YWKFromLoginFindBackVC) {
            
            self.HUD.labelText = NSLocalizedString(@"找回密码成功", nil);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)),
                           dispatch_get_main_queue(), ^{
                               
                               [self.HUD removeFromSuperview];
                               [self.navigationController popViewControllerAnimated:YES];
                           });
            return;
        }
        self.HUD.labelText = NSLocalizedString(@"修改成功", nil);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)),
                       dispatch_get_main_queue(), ^{
                           
           [self.HUD removeFromSuperview];
           [self.navigationController popViewControllerAnimated:YES];
        });
    }
    else
    {
        [self.HUD removeFromSuperview];
        if(result == -1){
            
            [self showAlertWithString:NSLocalizedString(@"无效验证码", nil)];
        }
        else if(result == 11)
        {
            [self showAlertWithString:NSLocalizedString(@"帐号或手机号没注册", nil)];
        }
        else if(result == 28)
        {
            [self showAlertWithString:@"一天只能修改五次密码"];
        }
        else if(result == 3)
        {
            [self showAlertWithString:response[@"remark"]];
        }
    }
}

-(void)failedWithResponse:(id)response{
    
    self.identifyingBtn.userInteractionEnabled = YES;
    [self showAlertWithString:response[@"errorReason"]];
    [self.HUD hide:YES];
    [self.HUD removeFromSuperview];
}


#pragma mark - YWKLoginHander

- (void)loginSuccessForAccount:(YWKUser *)account
{
    DDLogInfo(@"account.userName = %@,account.loginName = %@, account.displayName = %@, account.password = %@",
          account.userName,account.loginName, account.displayName, account.password);
    self.HUD.mode = MBProgressHUDModeCustomView;
    self.HUD.labelText = @"登录成功";
    UIImage *image = [UIImage imageNamed:@"checkout_white"];
    UIImageView * imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    imageview.image = image;
    self.HUD.customView = imageview;
    self.HUD.labelFont = [UIFont fontWithName:kTextFontName
                                         size:[FiexibleTool floatWithOriginalFloat:14.f
                                                                  isFlexibleHeight:YES]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.HUD removeFromSuperview];
        [self dismissViewControllerAnimated:YES completion:nil];
    });
    
}

- (void)loginFailedForAccount:(YWKUser *)account withError:(NSError *)error
{
    [self.HUD hide:YES];
    [self.HUD removeFromSuperview];
    DDLogInfo(@"loginFailedForAccount error = %@", [error localizedDescription]);
    [self showAlertWithString:error.userInfo[@"description"]];
}



#pragma mark - UITextField delegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
                                                       replacementString:(NSString *)string
{
    if (textField.tag == kPhoneNumberTag)
    {
        if (self.pwdType == YWKFromLoginRegistVC)
        {
            if (string.length > 0)
            {
                if ((textField.text.length == 3)
                    ||(textField.text.length == 8))
                {
                    textField.text = [NSString stringWithFormat:@"%@ ", textField.text];
                }
                else if(textField.text.length == 13)
                {
                    [self.view endEditing:YES];
                }
            }
        }
    }
    return YES;
}


#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL
         inRange:(NSRange)characterRange {
    
    if ([[URL scheme] isEqualToString:@"YWK"])
    {
        self.serviceTerms.y = 12;
        self.serviceTerms.alpha = 1.0;
        [self.view addSubview:self.serviceTerms];
        return NO;
    }
    return YES;
}

- (void)popVC
{
   [self.navigationController popViewControllerAnimated:YES];
}

- (void)cancelAction
{
    _isAccoutChecking = YES;
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 *  @brief 在注册成功之后初始化新用户
 */
- (void)configNewUser:(NSString *)name password:(NSString *)pwd
          phoneNumber:(NSString *)phoneNumber
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),
                   dispatch_get_main_queue(), ^{
        
         self.HUD.labelText = @"正在登录";
    });
   
    //登录
    YWKUser *loginUser = [[YWKUser alloc] initWithLoginName:self.phoneNumber];
    self.loginHander = [YWKLoginHander loginHandler];
    [self.loginHander loginHandlerForAccount:loginUser withDelegate:self error:nil];
    [self.loginHander startLoginWithPassword:self.pwdTextField.text withError:nil];

    //本地添加用户
    //    ICTUserEntity * user = [[ICTUserEntity alloc] initWithName:[name lowercaseString]];
    //    user.role = ICTUserRoleLogining;
    //    user.selectedMonitorID = kICTMonitorTemplateIDMax;
    //    user.phone = phoneNumber;
    //    user.password = pwd;
    //    [[ICTSettingManager sharedSettingManager] setUsername:user.name andPassword:pwd];
    //
    //    ICTBathDao *bathDao = [ICTBathDao sharedBathDao];
    //    [bathDao replaceUser:user withSuccess:nil failed:nil];
    
    //设置当前用户，以供后面几个页面使用 renyi
    //    [ICTRuntimeInfo sharedRuntimeInfo].currentUser = user;
}


#pragma mark  点击发送验证码

- (void)identifyingCodeClick
{
    RealReachability * real = [RealReachability sharedInstance];
    if(real.currentReachabilityStatus == RealStatusUnknown ||
       real.currentReachabilityStatus == RealStatusNotReachable)
    {
        [MBProgressHUD hubWithTitle:NSLocalizedString(@"网络不给力，稍后再试试",nil)
                             second:3
                             toView:self.view];
        return;
    }
    self.identifyingBtn.userInteractionEnabled = NO;
    [self hidenKeyboard];
    if (self.pwdType == YWKFromLoginRegistVC)    //注册
    {
        [self fetchIdentifyingCodeForReigister];
    }
    else if(self.pwdType == YWKFromLoginFindBackVC) //找回密码
    {
        [self fetchIdentifyingCodeForForgetPwd];
    }
    else //修改密码
    {
        [self fetchIdentifyingCodeForReigister];
    }
}


#pragma mark - 获取注册验证码
// 手机号
- (void)fetchIdentifyingCodeForReigister
{
    self.phoneNumber = [self.phoneNumberTextField.text
                        stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (self.pwdType == YWKFromSetVC) {
        
        self.loginHander = [YWKLoginHander loginHandler];
        NSString * user = self.loginHander.account.userName;
        [self showAlertWithString:NSLocalizedString(@"手机验证码已发送请查看手机短信", nil)];
        [self countDown];
        [self.dataModel requestVerificationCodeAPIWithName:user];
        return;
    }
    if (self.phoneNumber.length == 0)
    {
        [self showAlertWithString:NSLocalizedString(@"请输入手机号！",
                                                    nil)];
        self.identifyingBtn.userInteractionEnabled = YES;
        return ;
    }
        //判断账号是否是11位手机号码
    if (![YWKHelper isValidatePhoneNmuber:self.phoneNumber])
    {
        [self showAlertWithString:NSLocalizedString(@"请输入正确的手机号！",
                                                    nil)];
        self.identifyingBtn.userInteractionEnabled = YES;
        return ;
    }
    else
   {
    //判断手机号 是否存在
    [self fetchTelephoneNumber:self.phoneNumber];
    }
}


/**
 *  用户名是否存在，包含有手机号，也可以不用手机号判断
 *
 *  @param userName
 */
- (void)fetchUsername:(NSString *)userName
{
    [self.dataModel fetchDataWithUserName:userName];
}

// 手机号判断是否存在
- (void)fetchTelephoneNumber:(NSString *)telephone
{
    [self.dataModel fetchDataWithTelephone:self.phoneNumber];
}



#pragma mark - 找回密码验证账户是否存在
- (void)fetchIdentifyingCodeForForgetPwd
{
    self.phoneNumber = self.phoneNumberTextField.text;
    if(![YWKHelper isValidateUsername:self.phoneNumber])
    {
        [self showAlertWithString:NSLocalizedString(@"请输入手机号码/帐号！", nil)];
        self.identifyingBtn.userInteractionEnabled = YES;
        return;
    }
    
    [self fetchUsername:self.phoneNumber];
}


- (void)countDown
{
    _count = 0;
    _timer = [[NSTimer alloc]init];
    self.identifyingBtn.hidden = YES;
    self.countDownTimer.hidden = NO;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                              target:self
                                            selector:@selector(countTime)
                                    userInfo:nil repeats:YES];
}

- (void)showAlertWithString:(NSString *)alertString
{
    [MBProgressHUD hubWithTitle:NSLocalizedString(alertString, nil)
                         second:2
                         toView:self.view];
}

- (void)countTime
{
    _count++;
    self.identifyingBtn.hidden = YES;
    [self.countDownTimer setText:[NSString stringWithFormat:NSLocalizedString(@"%d%@",
                                                                              nil),
                                  (zCodeCountTime - _count),
                                  NSLocalizedString(@"S", nil)]];
    if (zCodeCountTime == _count)
    {
        self.countDownTimer.text = NSLocalizedString(@"60S", nil);
    }
    if (_count >= zCodeCountTime)
    {
        [_timer invalidate];
        [self reSendIdentifyCode];
    }
}

- (void)reSendIdentifyCode
{
    self.identifyingBtn.hidden = NO;
    self.identifyingBtn.userInteractionEnabled = YES;
    self.countDownTimer.hidden = YES;
    self.identifyingBtn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:16];
    [self.identifyingBtn setTitle:NSLocalizedString(@"重新获取", nil)
                         forState:UIControlStateNormal];
}

- (void)registerClick
{
    [self hidenKeyboard];
//    //判断网络
    RealReachability * real = [RealReachability sharedInstance];
    if(real.currentReachabilityStatus == RealStatusUnknown ||
       real.currentReachabilityStatus == RealStatusNotReachable)
    {
        [MBProgressHUD hubWithTitle:NSLocalizedString(@"网络不给力，稍后再试试", nil)
                             second:3
                             toView:self.view];
        return;
    }
    //输入有效性判断
    if ([self isInputLegal])
    {
        if(!_userN)
        {
            _userN = [NSString string];
        }
        
        _userN = _phoneNumber;//找回密码页面+注册页面
        
        //注册界面 弹用户协议
        if (self.pwdType == YWKFromLoginRegistVC)
        {
            self.serviceTerms.y = 12;
            self.serviceTerms.alpha = 1.0;
            [self.view addSubview:self.serviceTerms];;
        }
        else if (self.pwdType == YWKFromSetVC)//修改密码
        {
            [self agreeAction];
        }
        else//找回密码
        {
            //避免输入中文崩溃
            if(![YWKHelper isValidateUsername:self.phoneNumber])
            {
                [self showAlertWithString:NSLocalizedString(@"请输入手机号码/帐号", nil)];
                return;
            }
            [self agreeAction];
        }
    }
}



#pragma mark - 找回密码页面点击确定：button显示为确定
- (void)agreeAction
{
    RealReachability * real = [RealReachability sharedInstance];
    if(real.currentReachabilityStatus == RealStatusUnknown ||
       real.currentReachabilityStatus == RealStatusNotReachable)
    {
        [MBProgressHUD hubWithTitle:NSLocalizedString(@"网络不给力，稍后再试试",nil)
                             second:3
                             toView:self.view];
        return;
    }
    NSString * verificationCode = self.identifyingCodeTF.text;
    NSString * password = self.pwdTextField.text;
    self.HUD = [[MBProgressHUD alloc]initWithView:self.view];
    self.HUD.activityIndicatorColor = kYWKNavBarTitleColor;
    self.HUD.labelText = NSLocalizedString(@"修改密码", nil);
    [self.view addSubview:self.HUD];
    [self.HUD show:YES];
    if (self.pwdType == YWKFromSetVC) {
        
        [self.dataModel modifyPasswordWithPhoneOrName:self.loginHander.account.userName
                                     verificationCode:verificationCode
                                             password:password];
        return;
    }
    [self.dataModel modifyPasswordWithPhoneOrName:self.phoneNumber
                                 verificationCode:verificationCode
                                         password:password];
}

#pragma mark  - 注册

// 手机号/用户名
- (void)registAction
{
    self.phoneNumber = [self.phoneNumberTextField.text stringByReplacingOccurrencesOfString:@" "
                                                                                 withString:@""];
    NSString * verificationCode = self.identifyingCodeTF.text;
    NSString * password = self.pwdTextField.text;
    
    self.HUD = [[MBProgressHUD alloc]initWithView:self.view];
    self.HUD.labelText = NSLocalizedString(@"正在注册", nil);
    self.HUD.activityIndicatorColor = kYWKButtonBlueColor;
    [self.view addSubview:self.HUD];
    [self.HUD show:YES];
    [self.dataModel registerWithTelephone:self.phoneNumber
                         verificationCode:verificationCode
                                 password:password
                                 roleType:YWKPatient];
}


- (BOOL)isInputLegal
{
    self.identifyingCodeTF.text = [self.identifyingCodeTF.text removeHeadAndFootSpace];
    self.phoneNumber = [NSString stringWithFormat:@"%@", [self.phoneNumberTextField.text stringByReplacingOccurrencesOfString:@" "
                                                                              withString:@""]];
    //1、注册时判断是否手机号
    if (self.pwdType == YWKFromLoginRegistVC)
    {
        if (self.phoneNumber.length == 0)
        {
            [self showAlertWithString:NSLocalizedString(@"请输入手机号", nil)];
            return NO;
        }
        if (![YWKHelper isValidatePhoneNmuber:self.phoneNumber] )
        {
            [self showAlertWithString:NSLocalizedString(@"请输入正确的手机号", nil)];
            return NO;
        }
    }
    //判断是否为空
    if (self.pwdType == YWKFromLoginFindBackVC)
    {
        if (self.phoneNumber.length == 0)
        {
            [self showAlertWithString:NSLocalizedString(@"请输入手机号/帐号", nil)];
            return NO;
        }
    }
    
    //2、判断验证码
    if (self.identifyingCodeTF.text.length != 6)
    {
        [self showAlertWithString:NSLocalizedString(@"请输入6位验证码!", nil)];
        return NO;
    }
    //3、判断密码
    else if (![YWKHelper isValidatePassword:self.pwdTextField.text])
    {
        [self showAlertWithString:NSLocalizedString(@"请输入6-15位数字或字母!", nil)];
        return NO;
    }
    else if (![YWKHelper isValidatePassword:self.ensurePwdTextField.text])
    {
        [self showAlertWithString:NSLocalizedString(@"请输入6-15位数字或字母!", nil)];
        return NO;
    }
    //4、判断两次密码是否一致
    else if (![self.pwdTextField.text isEqualToString:self.ensurePwdTextField.text])
    {
        [self showAlertWithString:NSLocalizedString(@"2次输入的密码不一致，请重试!", nil)];
        return NO;
    }
    else
    {
        return YES;
    }
}



#pragma mark - event response

- (void)serviceTermsDisAgreeClick
{
    [self.serviceTerms removeFromSuperview];
}

- (void)serviceTermsAgreeClick
{

    [UIView animateWithDuration:0.3f
                          delay:0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^
     {
         self.serviceTerms.alpha = 0.1f;
         self.serviceTerms.y = UI_SCREEN_HEIGHT;
     }
                     completion:^(BOOL finished)
     {
         [self.serviceTerms removeFromSuperview];
     }];
    [self registAction];
}

#pragma mark - private methods

//隐藏键盘
- (void)hidenKeyboard
{
    [self.identifyingCodeTF resignFirstResponder];
    [self.pwdTextField resignFirstResponder];
    [self.phoneNumberTextField resignFirstResponder];
    [self.ensurePwdTextField resignFirstResponder];
}

//点击键盘上的return按钮响应的方法
- (void)nextOnKeyboard:(UITextField *)textfield
{
    if (textfield == self.phoneNumberTextField) {
        [self.identifyingCodeTF becomeFirstResponder];
    }
    else if (textfield == self.identifyingCodeTF)
    {
        [self.pwdTextField becomeFirstResponder];
    }
    else if (textfield == self.pwdTextField)
    {
        [self.ensurePwdTextField becomeFirstResponder];
    }
    else if (textfield == self.ensurePwdTextField)
    {
        [self hidenKeyboard];
    }
}

- (void)addConstraint{
    
    // 约束
    [YWKHelper addConstraintInFirstItem:self.rigisterScrollView
                           toSecondItem:self.view
                               topSpace:0 leftSpace:0 rightSpace:0
                            bottomSpace:0 superView:nil];
    
    // content
    [YWKHelper addConstraintInFirstItem:self.scrollContentView
                           toSecondItem:self.rigisterScrollView
                               topSpace:0 leftSpace:0 rightSpace:0
                            bottomSpace:0 superView:nil];
    
    [YWKHelper onlyAddVerticalCentreYConstraintInFirstItem:self.scrollContentView
                                              toSecondItem:self.rigisterScrollView];
    [YWKHelper onlyAddVerticalCentreXConstraintInFirstItem:self.scrollContentView
                                              toSecondItem:self.rigisterScrollView];
    
    if (self.pwdType != YWKFromSetVC) {

    // lab
    [YWKHelper addConstraintInFirstItem:self.phoneNumberLable
                           toSecondItem:self.scrollContentView
                               topSpace:0 leftSpace:10
                                 height:42
                                  width:labWidth
                              superView:nil];
    [YWKHelper addConstraintInFirstItem:self.identifyingCodeLable
                           toSecondItem:self.scrollContentView
                               topSpace:42 + 1 leftSpace:10
                                 height:42
                                  width:labWidth
                              superView:nil];
    [YWKHelper addConstraintInFirstItem:self.pwdLable
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 2 + 1
                              leftSpace:10
                                 height:42
                                  width:labWidth superView:nil];
    [YWKHelper addConstraintInFirstItem:self.ensurePwdLabel
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 3 + 1 leftSpace:10
                                 height:42
                                  width:labWidth
                              superView:nil];
    
    // line
    [YWKHelper addConstraintInFirstItem:self.phoneSpareLine
                           toSecondItem:self.scrollContentView
                               topSpace:42 leftSpace:10
                             rightSpace:-10
                                 height:1 superView:nil];
    
    [YWKHelper addConstraintInFirstItem:self.identySpareLine
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 2 leftSpace:10
                             rightSpace:-10
                                 height:1 superView:nil];
    [YWKHelper addConstraintInFirstItem:self.pwdSpareLine
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 3 leftSpace:10
                             rightSpace:-10
                                 height:1 superView:nil];
    [YWKHelper addConstraintInFirstItem:self.ensurePwdSpareLine
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 4 leftSpace:0
                             rightSpace:0
                                 height:1 superView:nil];
    
    //
    [YWKHelper addConstraintInFirstItem:self.userAgreementTV
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 4 + 1
                              leftSpace:0
                             rightSpace:0
                                 height:42
                              superView:nil];
    
    // textField
    [YWKHelper addConstraintInFirstItem:self.phoneNumberTextField
                           toSecondItem:self.scrollContentView
                               topSpace:0
                              leftSpace:10 + labWidth
                             rightSpace:-10
                                 height:42 superView:nil];
    [YWKHelper addConstraintInFirstItem:self.identifyingCodeTF
                           toSecondItem:self.scrollContentView
                               topSpace:42 + 1 leftSpace:10 + labWidth
                             rightSpace:-100 height:42 superView:nil];
    [YWKHelper addConstraintInFirstItem:self.pwdTextField
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 2 + 1 leftSpace:10 + labWidth
                             rightSpace:-10 height:42 superView:nil];
    [YWKHelper addConstraintInFirstItem:self.ensurePwdTextField
                           toSecondItem:self.scrollContentView
                               topSpace:42 * 3 + 1 leftSpace:10 + labWidth
                             rightSpace:-10 height:42 superView:nil];
    }else{
        
        self.phoneNumberLable.hidden = YES;
        self.phoneNumberTextField.hidden = YES;
//        // lab
//        [YWKHelper addConstraintInFirstItem:self.phoneNumberLable
//                               toSecondItem:self.scrollContentView
//                                   topSpace:0 leftSpace:10
//                                     height:42
//                                      width:labWidth
//                                  superView:nil];
        [YWKHelper addConstraintInFirstItem:self.identifyingCodeLable
                               toSecondItem:self.scrollContentView
                                   topSpace:0 leftSpace:10
                                     height:42
                                      width:labWidth
                                  superView:nil];
        [YWKHelper addConstraintInFirstItem:self.pwdLable
                               toSecondItem:self.scrollContentView
                                   topSpace:42 * 1 + 1
                                  leftSpace:10
                                     height:42
                                      width:labWidth superView:nil];
        [YWKHelper addConstraintInFirstItem:self.ensurePwdLabel
                               toSecondItem:self.scrollContentView
                                   topSpace:42 * 2 + 1 leftSpace:10
                                     height:42
                                      width:labWidth
                                  superView:nil];
        
        // line
//        [YWKHelper addConstraintInFirstItem:self.phoneSpareLine
//                               toSecondItem:self.scrollContentView
//                                   topSpace:42 leftSpace:10
//                                 rightSpace:-10
//                                     height:1 superView:nil];
        
        [YWKHelper addConstraintInFirstItem:self.identySpareLine
                               toSecondItem:self.scrollContentView
                                   topSpace:42 * 1 leftSpace:10
                                 rightSpace:-10
                                     height:1 superView:nil];
        [YWKHelper addConstraintInFirstItem:self.pwdSpareLine
                               toSecondItem:self.scrollContentView
                                   topSpace:42 * 2 leftSpace:10
                                 rightSpace:-10
                                     height:1 superView:nil];
        [YWKHelper addConstraintInFirstItem:self.ensurePwdSpareLine
                               toSecondItem:self.scrollContentView
                                   topSpace:42 * 3 leftSpace:0
                                 rightSpace:0
                                     height:1 superView:nil];

        [YWKHelper addConstraintInFirstItem:self.identifyingCodeTF
                               toSecondItem:self.scrollContentView
                                   topSpace:0 leftSpace:10 + labWidth
                                 rightSpace:-100 height:42 superView:nil];
        [YWKHelper addConstraintInFirstItem:self.pwdTextField
                               toSecondItem:self.scrollContentView
                                   topSpace:42 * 1 + 1 leftSpace:10 + labWidth
                                 rightSpace:-10 height:42 superView:nil];
        [YWKHelper addConstraintInFirstItem:self.ensurePwdTextField
                               toSecondItem:self.scrollContentView
                                   topSpace:42 * 2 + 1 leftSpace:10 + labWidth
                                 rightSpace:-10 height:42 superView:nil];
    }
    
    
    // 验证码
    [self.identifyingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.scrollContentView).offset([FiexibleTool floatWithOriginalFloat:-16.f
                                                                              isFlexibleHeight:YES]);
        make.size.mas_equalTo(CGSizeMake([FiexibleTool floatWithOriginalFloat:90.f
                                                             isFlexibleHeight:YES],
                                         [FiexibleTool floatWithOriginalFloat:28.f
                                                             isFlexibleHeight:YES]));
        make.centerY.equalTo(self.identifyingCodeTF);
    }];
    
    [self.countDownTimer mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.scrollContentView).offset([FiexibleTool floatWithOriginalFloat:-16.f
                                                                              isFlexibleHeight:YES]);
        make.size.mas_equalTo(CGSizeMake([FiexibleTool floatWithOriginalFloat:60.f isFlexibleHeight:YES],
                                         [FiexibleTool floatWithOriginalFloat:28.f isFlexibleHeight:YES]));
        make.centerY.equalTo(self.identifyingCodeTF);
    }];
    
    // 注册 or 确定
    [self.rigterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.scrollContentView).offset([FiexibleTool floatWithOriginalFloat:-17
                                                                               isFlexibleHeight:YES]);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake([FiexibleTool floatWithOriginalFloat:300 isFlexibleHeight:YES],
                                         [FiexibleTool floatWithOriginalFloat:42 isFlexibleHeight:YES]));
    }];
}


#pragma mark - getters and setters

#pragma mark - 用户协议
- (UIView *)serviceTerms
{
    if (!_serviceTerms)
    {
        _serviceTerms = [[UIView alloc] initWithFrame:CGRectMake(18,
                                                                 12,
                                                                 UI_SCREEN_WIDTH - 36,
                                                                 UI_SCREEN_HEIGHT -IOS9VIEWFRAMEORIGIN-32)];
        _serviceTerms.backgroundColor = [UIColor colorWithHex:0xb4c8d6 alpha:1.0];
        
        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,
                                                                         0,
                                                                         CGRectGetWidth(_serviceTerms.frame),
                                                                         CGRectGetHeight(_serviceTerms.frame) - 44)];
        webView.opaque = NO;
        webView.backgroundColor = [UIColor clearColor];
        NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];
        NSString *filePath;
        if (kIsChinese) {
            
            filePath  = [resourcePath stringByAppendingPathComponent:@"protocol_zh-Hans.html"];
        }
        else{
            
            filePath  = [resourcePath stringByAppendingPathComponent:@"protocol_en.html"];
        }
//        NSString *htmlstring =[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:filePath]];
        [webView loadRequest:request];
    
//        [webView loadHTMLString:htmlstring  baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];
        [_serviceTerms addSubview:webView];
        
        YWKDrawLineView *hLine = [[YWKDrawLineView alloc]initWithFrame:CGRectMake(0,
                                                                                  CGRectGetHeight(webView.frame),
                                                                                  CGRectGetWidth(_serviceTerms.frame),
                                                                                  1.0)];
        hLine.backgroundColor = [UIColor grayColor];
        [_serviceTerms addSubview:hLine];
        
        YWKDrawLineView *vLine = [[YWKDrawLineView alloc]
                                  initWithFrame:CGRectMake(CGRectGetWidth(_serviceTerms.frame) / 2.0,
                                                                                  CGRectGetMaxY(hLine.frame),
                                                                                  1.0,
                                                                                  [FiexibleTool floatWithOriginalFloat:44.f
                                                                                                      isFlexibleHeight:YES])];
        vLine.backgroundColor = [UIColor grayColor];
        [_serviceTerms addSubview:vLine];
        
        UIButton *disAgreeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        disAgreeBtn.frame = CGRectMake(5,
                                       CGRectGetHeight(_serviceTerms.frame) - 40,
                                       CGRectGetWidth(_serviceTerms.frame) / 2.0 - 10,
                                       40);
        disAgreeBtn.titleLabel.font = [UIFont fontWithName:kTextFontName size:16.0];
        [disAgreeBtn setTitle:NSLocalizedString(@"取消", @"取消") forState:UIControlStateNormal];
        [disAgreeBtn setBackgroundColor:[UIColor colorWithHex:0xb4c8d6 alpha:1.0]];
        [disAgreeBtn addTarget:self
                        action:@selector(serviceTermsDisAgreeClick)
              forControlEvents:UIControlEventTouchUpInside];
        
        [_serviceTerms addSubview:disAgreeBtn];
        
        UIButton *agreeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        agreeBtn.frame = CGRectMake(CGRectGetWidth(_serviceTerms.frame) / 2.0 + 5,
                                    CGRectGetHeight(_serviceTerms.frame) - 40,
                                    CGRectGetWidth(_serviceTerms.frame) / 2.0 - 10,
                                    40);
        agreeBtn.titleLabel.font = [UIFont fontWithName:kTextFontName size:16.0];
        
        [agreeBtn setTitle:NSLocalizedString(@"同意", @"同意") forState:UIControlStateNormal];
        [agreeBtn setBackgroundColor:[UIColor colorWithHex:0xb4c8d6 alpha:1.0]];
        [agreeBtn addTarget:self
                     action:@selector(serviceTermsAgreeClick)
           forControlEvents:UIControlEventTouchUpInside];
        _serviceTerms.layer.masksToBounds = YES;
        _serviceTerms.layer.cornerRadius = 4.0f;
        [_serviceTerms addSubview:agreeBtn];
    }
    return _serviceTerms;
}



- (UIScrollView *)rigisterScrollView
{
    if (!_rigisterScrollView)
    {
        _rigisterScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _rigisterScrollView.backgroundColor = [UIColor clearColor];
        _rigisterScrollView.delegate = self;
        _rigisterScrollView.showsVerticalScrollIndicator = NO;
        _rigisterScrollView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _rigisterScrollView;
}


- (UIView *)scrollContentView
{
    if (!_scrollContentView) {
        _scrollContentView = [[UIView alloc] initWithFrame:CGRectZero];
        _scrollContentView.backgroundColor = [UIColor clearColor];
        _scrollContentView.translatesAutoresizingMaskIntoConstraints = NO;
        
        //注册页面有C＋图
        if (self.pwdType == YWKFromLoginRegistVC)
        {
            //            [_rigisterScrollView addSubview:self.yWKLogoView];
        }
        
        //各个输入项
        [_scrollContentView addSubview:self.phoneNumberTextField];
        [_scrollContentView addSubview:self.identifyingCodeTF];
        [_scrollContentView addSubview:self.pwdTextField];
        [_scrollContentView addSubview:self.ensurePwdTextField];
        
        //各输入项的label
        [_scrollContentView addSubview:self.phoneNumberLable];
        [_scrollContentView addSubview:self.identifyingCodeLable];
        [_scrollContentView addSubview:self.pwdLable];
        [_scrollContentView addSubview:self.ensurePwdLabel];
        
        
        if (self.pwdType == YWKFromSetVC) {
            
            self.phoneSpareLine.hidden = YES;
        }
        [_scrollContentView addSubview:self.phoneSpareLine];
        [_scrollContentView addSubview:self.identySpareLine];
        [_scrollContentView addSubview:self.pwdSpareLine];
        [_scrollContentView addSubview:self.ensurePwdSpareLine];
        
        //
        [_scrollContentView addSubview:self.userAgreementTV];
        
        //获取验证码
        [_scrollContentView addSubview:self.identifyingBtn];
        [_scrollContentView addSubview:self.countDownTimer];
        
        //添加手势
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]
                                           initWithTarget:self
                                           action:@selector(hidenKeyboard)];
        gesture.numberOfTapsRequired = 1;
        [_scrollContentView addGestureRecognizer:gesture];
        
        //创建帐号按钮
        [_scrollContentView addSubview:self.rigterBtn];
    }
    return _scrollContentView;
}

- (YWKDrawLineView *)phoneSpareLine
{
    if (!_phoneSpareLine) {
        
        _phoneSpareLine = [[YWKDrawLineView alloc] initWithFrame:CGRectZero];
        _phoneSpareLine.translatesAutoresizingMaskIntoConstraints = NO;
        _phoneSpareLine.backgroundColor = kPlaceHolderColor;
    }
    return _phoneSpareLine;
}

- (YWKDrawLineView *)identySpareLine
{
    if (!_identySpareLine) {
        
        _identySpareLine = [[YWKDrawLineView alloc] initWithFrame:CGRectZero];
        _identySpareLine.translatesAutoresizingMaskIntoConstraints = NO;
        _identySpareLine.backgroundColor = kPlaceHolderColor;
    }
    return _identySpareLine;
}

- (YWKDrawLineView *)pwdSpareLine
{
    if (!_pwdSpareLine) {
        
        _pwdSpareLine = [[YWKDrawLineView alloc] initWithFrame:CGRectZero];
        _pwdSpareLine.translatesAutoresizingMaskIntoConstraints = NO;
        _pwdSpareLine.backgroundColor = kPlaceHolderColor;
    }
    return _pwdSpareLine;
}

- (YWKDrawLineView *)ensurePwdSpareLine
{
    if (!_ensurePwdSpareLine) {
        
        _ensurePwdSpareLine = [[YWKDrawLineView alloc] initWithFrame:CGRectZero];
        _ensurePwdSpareLine.translatesAutoresizingMaskIntoConstraints = NO;
        _ensurePwdSpareLine.backgroundColor = kPlaceHolderColor;
    }
    return _ensurePwdSpareLine;
}


//iCareTech Logo View
- (UIImageView *)yWKLogoView
{
    if (!_yWKLogoView) {
        
        UIImage *logoImg = [UIImage imageNamed:@"iCTLogo"];
        _yWKLogoView = [[UIImageView alloc]initWithFrame:CGRectMake(119,
                                                                    22,
                                                                    logoImg.size.width,
                                                                    logoImg.size.height)];
        _yWKLogoView.image = logoImg;
    }
    
    return _yWKLogoView;
}

//手机号输入
- (YWKCustomTextField *)phoneNumberTextField
{
    if (!_phoneNumberTextField) {
        
        _phoneNumberTextField = [[YWKCustomTextField alloc]initWithFrame:CGRectZero];
        _phoneNumberTextField.translatesAutoresizingMaskIntoConstraints = NO;
        _phoneNumberTextField.delegate = self;
        _phoneNumberTextField.borderStyle = UITextBorderStyleNone;
        _phoneNumberTextField.backgroundColor = [UIColor clearColor];
        _phoneNumberTextField.textColor = [UIColor blackColor];
        if (self.pwdType == YWKFromLoginRegistVC || self.pwdType == YWKFromSetVC)
        {
            _phoneNumberTextField.placeholder = NSLocalizedString(@"请输入手机号码",nil);
            _phoneNumberTextField.keyboardType = UIKeyboardTypeNumberPad;
        }
        else
        {
            _phoneNumberTextField.placeholder = NSLocalizedString(@"请输入手机号/帐号",nil);
        }
        _phoneNumberTextField.placeHolderColor = kPlaceHolderColor;
        _phoneNumberTextField.placeHolderSize = 16.0f;
        _phoneNumberTextField.clearButtonMode= UITextFieldViewModeWhileEditing;
        [_phoneNumberTextField addTarget:self
                                  action:@selector(nextOnKeyboard:)
                        forControlEvents:UIControlEventEditingDidEndOnExit];
        _phoneNumberTextField.tag = kPhoneNumberTag;
    }
    return _phoneNumberTextField;
}

//验证码输入
- (YWKCustomTextField *)identifyingCodeTF
{
    if (!_identifyingCodeTF) {
        
        _identifyingCodeTF = [[YWKCustomTextField alloc]initWithFrame:CGRectZero];
        _identifyingCodeTF.translatesAutoresizingMaskIntoConstraints = NO;
        _identifyingCodeTF.delegate = self;
        _identifyingCodeTF.borderStyle = UITextBorderStyleNone;
        _identifyingCodeTF.backgroundColor = [UIColor clearColor];
        _identifyingCodeTF.textColor = [UIColor blackColor];
        _identifyingCodeTF.placeholder = NSLocalizedString(@"", nil);
        _identifyingCodeTF.placeHolderColor = kPlaceHolderColor;
        _identifyingCodeTF.placeHolderSize = 16.0f;
        _identifyingCodeTF.clearButtonMode= UITextFieldViewModeWhileEditing;
        _identifyingCodeTF.clearsOnBeginEditing = NO;
        _identifyingCodeTF.keyboardType = UIKeyboardTypeNumberPad;
        [_identifyingCodeTF addTarget:self
                               action:@selector(nextOnKeyboard:)
                     forControlEvents:UIControlEventEditingDidEndOnExit];
    }
    
    return _identifyingCodeTF;
}

//设置密码输入
- (YWKCustomTextField *)pwdTextField
{
    if (!_pwdTextField) {
        
        _pwdTextField = [[YWKCustomTextField alloc]initWithFrame:CGRectZero];
        _pwdTextField.translatesAutoresizingMaskIntoConstraints = NO;
        _pwdTextField.delegate = self;
        _pwdTextField.borderStyle = UITextBorderStyleNone;
        _pwdTextField.backgroundColor = [UIColor clearColor];
        _pwdTextField.textColor = [UIColor blackColor];
        _pwdTextField.placeholder = NSLocalizedString(@"6-15位数字或字母", nil);
        _pwdTextField.placeHolderColor = kPlaceHolderColor;
        _pwdTextField.placeHolderSize = 16.0f;
        _pwdTextField.clearButtonMode= UITextFieldViewModeWhileEditing;
        _pwdTextField.secureTextEntry = YES;
        _pwdTextField.returnKeyType = UIReturnKeyNext;
        [_pwdTextField addTarget:self
                          action:@selector(nextOnKeyboard:)
                forControlEvents:UIControlEventEditingDidEndOnExit];
    }
    return _pwdTextField;
}

//确认密码输入
- (YWKCustomTextField *)ensurePwdTextField
{
    if (!_ensurePwdTextField) {
        
        _ensurePwdTextField = [[YWKCustomTextField alloc]initWithFrame:CGRectZero];
        _ensurePwdTextField.translatesAutoresizingMaskIntoConstraints = NO;
        _ensurePwdTextField.delegate = self;
        _ensurePwdTextField.borderStyle = UITextBorderStyleNone;
        _ensurePwdTextField.backgroundColor = [UIColor clearColor];
        _ensurePwdTextField.textColor = [UIColor blackColor];
        _ensurePwdTextField.placeholder = NSLocalizedString(@"请再次输入密码", nil);
        _ensurePwdTextField.placeHolderColor = kPlaceHolderColor;
        _ensurePwdTextField.placeHolderSize = 16.0f;
        _ensurePwdTextField.clearButtonMode= UITextFieldViewModeWhileEditing;
        _ensurePwdTextField.secureTextEntry = YES;
        _ensurePwdTextField.returnKeyType = UIReturnKeyNext;
        [_ensurePwdTextField addTarget:self
                                action:@selector(nextOnKeyboard:)
                      forControlEvents:UIControlEventEditingDidEndOnExit];
    }
    return _ensurePwdTextField;
}



//手机号、验证码、密码、确认密码的label

- (UILabel *)phoneNumberLable
{
    if (!_phoneNumberLable) {
        
        _phoneNumberLable = [[UILabel alloc]initWithFrame:CGRectZero];
        _phoneNumberLable.translatesAutoresizingMaskIntoConstraints = NO;
        _phoneNumberLable.textAlignment = NSTextAlignmentLeft;
        _phoneNumberLable.font = [UIFont fontWithName:@"HelveticaNeue" size:
                                  [FiexibleTool floatWithOriginalFloat:16.f
                                                      isFlexibleHeight:YES]];
        _phoneNumberLable.textColor = [UIColor colorWithHex:0x42464c];
        if (self.pwdType == YWKFromLoginRegistVC || self.pwdType == YWKFromSetVC) {
            
            _phoneNumberLable.text = NSLocalizedString(@"手机号码", nil);
        }
        else if (self.pwdType == YWKFromLoginFindBackVC)
        {
            _phoneNumberLable.text = NSLocalizedString(@"帐号", @"帐号");
        }
    }
    return _phoneNumberLable;
}

- (UILabel *)identifyingCodeLable
{
    if (!_identifyingCodeLable) {
        
        _identifyingCodeLable = [[UILabel alloc]initWithFrame:CGRectZero];
        _identifyingCodeLable.translatesAutoresizingMaskIntoConstraints = NO;
        _identifyingCodeLable.textAlignment = NSTextAlignmentLeft;
        _identifyingCodeLable.font = [UIFont fontWithName:@"HelveticaNeue"
                                                     size:[FiexibleTool floatWithOriginalFloat:16.f
                                                                              isFlexibleHeight:YES]];
        _identifyingCodeLable.textColor = [UIColor colorWithHex:0x42464c];
        _identifyingCodeLable.text = NSLocalizedString(@"验证码", @"验证码");

    }
    
    return _identifyingCodeLable;
}

- (UILabel *)pwdLable
{
    if (!_pwdLable) {
        
        _pwdLable = [[UILabel alloc]initWithFrame:CGRectZero];
        _pwdLable.translatesAutoresizingMaskIntoConstraints = NO;
        _pwdLable.textAlignment = NSTextAlignmentLeft;
        _pwdLable.font = [UIFont fontWithName:@"HelveticaNeue"
                                         size:[FiexibleTool floatWithOriginalFloat:16.f
                                                                  isFlexibleHeight:YES]];
        _pwdLable.textColor = [UIColor colorWithHex:0x42464c];
        if(self.pwdType == YWKFromLoginRegistVC)
        {
            _pwdLable.text = NSLocalizedString(@"设置密码", @"设置密码");
        }
        else
        {
            _pwdLable.text = NSLocalizedString(@"设置密码", @"设置密码");
        }
    }
    return _pwdLable;
}

- (UILabel *)ensurePwdLabel
{
    if (!_ensurePwdLabel) {
        _ensurePwdLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _ensurePwdLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _ensurePwdLabel.textAlignment = NSTextAlignmentLeft;
        _ensurePwdLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:
                                [FiexibleTool floatWithOriginalFloat:16.f
                                                    isFlexibleHeight:YES]];
        _ensurePwdLabel.textColor = [UIColor colorWithHex:0x42464c];
        _ensurePwdLabel.text = NSLocalizedString(@"确认密码", @"确认密码");
    }
    
    return _ensurePwdLabel;
}

- (UITextView *)userAgreementTV
{
    if (!_userAgreementTV) {
        
        _userAgreementTV = [[UITextView alloc] initWithFrame:CGRectZero];
        _userAgreementTV.translatesAutoresizingMaskIntoConstraints = NO;
        _userAgreementTV.backgroundColor = [UIColor clearColor];
        _userAgreementTV.font = [UIFont fontWithName:@"HelveticaNeue" size:
                                 [FiexibleTool floatWithOriginalFloat:14.f
                                                     isFlexibleHeight:YES]];
        _userAgreementTV.textColor = [UIColor colorWithHex:0x42464c];
        _userAgreementTV.editable = NO;
        _userAgreementTV.selectable = YES;
        _userAgreementTV.delegate = self;
        _userAgreementTV.hidden = YES;
        _userAgreementTV.dataDetectorTypes = UIDataDetectorTypeLink;
        
        NSMutableAttributedString * mAttributeStr = [[NSMutableAttributedString alloc]
                                                     initWithString:agreeServiceTermsText];
        NSMutableParagraphStyle * paragrathStytle = [[NSMutableParagraphStyle alloc] init];
        
        paragrathStytle.alignment = NSTextAlignmentCenter;
        [mAttributeStr addAttribute:NSParagraphStyleAttributeName
                              value:paragrathStytle.copy
                              range:NSMakeRange(0, agreeServiceTermsText.length)];
        
        [mAttributeStr addAttribute:NSForegroundColorAttributeName
                              value:[UIColor colorWithHex:0xD5D6D9]
                              range:NSMakeRange(0, agreeServiceTermsText.length - YWKServiceTerms.length)];
        [mAttributeStr addAttribute:NSFontAttributeName
                              value:[UIFont fontWithName:@"HelveticaNeue" size:
                                     [FiexibleTool floatWithOriginalFloat:13.f
                                                         isFlexibleHeight:YES]]
                              range:NSMakeRange(0, agreeServiceTermsText.length - YWKServiceTerms.length)];
        
        NSRange  termsRange = [agreeServiceTermsText rangeOfString:YWKServiceTerms];
        [mAttributeStr addAttribute:NSForegroundColorAttributeName
                              value:[UIColor colorWithHex:0x009FE9] range:termsRange];
        [mAttributeStr addAttribute:NSFontAttributeName
                              value:[UIFont fontWithName:@"HelveticaNeue" size:
                                     [FiexibleTool floatWithOriginalFloat:13.f
                                                         isFlexibleHeight:YES]]
                              range:termsRange];
#warning 添加网址
        [mAttributeStr addAttribute:NSLinkAttributeName
                              value:@"YWK://useragreement"
                              range:termsRange];
        _userAgreementTV.attributedText = mAttributeStr.copy;
        
        if (_pwdType == YWKFromLoginRegistVC) {
            
            _userAgreementTV.hidden = NO;
        }
    }
    return _userAgreementTV;
}


- (UIButton *)identifyingBtn
{
    
    if (!_identifyingBtn)
    {
        _identifyingBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _identifyingBtn.frame = CGRectZero;
        _identifyingBtn.translatesAutoresizingMaskIntoConstraints = NO;
        [_identifyingBtn.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue"
                                                            size:[FiexibleTool floatWithOriginalFloat:13.f
                                                                                     isFlexibleHeight:YES]]];
        [_identifyingBtn setTitle:NSLocalizedString(@"获取验证码", nil)
                         forState:UIControlStateNormal];
        [_identifyingBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        _identifyingBtn.backgroundColor = [UIColor colorWithHex:0x009FE9];
        
        _identifyingBtn.layer.cornerRadius = [FiexibleTool floatWithOriginalFloat:13.F
                                                                 isFlexibleHeight:YES];
        _identifyingBtn.layer.masksToBounds = YES;
        [_identifyingBtn addTarget:self
                            action:@selector(identifyingCodeClick)
                  forControlEvents:UIControlEventTouchUpInside];
    }
    return _identifyingBtn;
}

- (UIButton *)rigterBtn
{
    if (!_rigterBtn) {
        //创建帐号按钮
        _rigterBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _rigterBtn.frame =CGRectZero;
        _rigterBtn.translatesAutoresizingMaskIntoConstraints = NO;
        [_rigterBtn.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue"
                                                       size:[FiexibleTool
                                                             floatWithOriginalFloat:16.f
                                                             isFlexibleHeight:YES]]];
        if (self.pwdType == YWKFromLoginRegistVC)
        {
            [_rigterBtn setTitle:NSLocalizedString(@"注册", @"注册")
                        forState:UIControlStateNormal];
        }
        else
        {
            [_rigterBtn setTitle:NSLocalizedString(@"确定", @"确定")
                        forState:UIControlStateNormal];
        }
        _rigterBtn.backgroundColor = [UIColor whiteColor];
        _rigterBtn.layer.borderColor = [UIColor colorWithHex:0x009FE9].CGColor;
        _rigterBtn.layer.borderWidth = 1.f;
        _rigterBtn.layer.cornerRadius = [FiexibleTool floatWithOriginalFloat:18.f
                                                            isFlexibleHeight:YES];
        _rigterBtn.layer.masksToBounds = YES;
        [_rigterBtn addTarget:self
                       action:@selector(registerClick)
             forControlEvents:UIControlEventTouchUpInside];
        [_rigterBtn setTitleColor:[UIColor colorWithHex:0x009FE9] forState:UIControlStateNormal];
    }
    
    return _rigterBtn;
}

- (UILabel *)countDownTimer
{
    if (!_countDownTimer)
    {
        _countDownTimer  = [[UILabel alloc] initWithFrame:CGRectZero];
        _countDownTimer.translatesAutoresizingMaskIntoConstraints = NO;
        _countDownTimer.font = [UIFont fontWithName:@"HelveticaNeue" size:14.f];
        _countDownTimer.text =  NSLocalizedString(@"60S",nil);
        _countDownTimer.textColor = [UIColor whiteColor];
        _countDownTimer.backgroundColor = [UIColor colorWithHex:0x009FE9];
        _countDownTimer.hidden = YES;
        _countDownTimer.layer.cornerRadius = [FiexibleTool floatWithOriginalFloat:13.F
                                                                 isFlexibleHeight:YES];
        _countDownTimer.layer.masksToBounds = YES;
        _countDownTimer.textAlignment = NSTextAlignmentCenter;
    }
    return _countDownTimer;
}

-(YWKLoginAndRegistViewModel *)viewModel{
    
    if (!_viewModel) {
        
        _viewModel = [[YWKLoginAndRegistViewModel alloc]init];
    }
    return _viewModel;
}

- (YWKLoginAndRegistModel *)dataModel{
   
    if (!_dataModel) {
        
        _dataModel = [[YWKLoginAndRegistModel alloc]init];
        _dataModel.loginAndRegistDelegate = self;
    }
    return _dataModel;
}
@end
